home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / DOS / dos_calls.c next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  25.5 KB  |  1,496 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <dos/dosextens.h>
  4. #include <dos/record.h>
  5.  
  6. #include <clib/exec_protos.h>
  7. #include <clib/dos_protos.h>
  8.  
  9. /* normally pragmas would be included here */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #define SAME 0
  15.  
  16. /* so the compiler doesn't get upset */
  17. void help(int *done);
  18. void quit(int *done);
  19. void source(int *done);
  20. void showbuffer(int *done);
  21. void open(int *done);
  22. void close(int *done);
  23. void showfiles(int *done);
  24. void lock(int *done);
  25. void unlock(int *done);
  26. void duplock(int *done);
  27. void showlocks(int *done);
  28. void myread(int *done);
  29. void mywrite(int *done);
  30. void lockrecord(int *done);
  31. void unlockrecord(int *done);
  32. void myfgets(int *done);
  33. void myfputs(int *done);
  34. void myfgetc(int *done);
  35. void myfputc(int *done);
  36. void myseek(int *done);
  37. void myflush(int *done);
  38. void dumpfh(int *done);
  39. void dumplock(int *done);
  40. void samelock(int*done);
  41. void execute(int *done);
  42. void deletefile(int *done);
  43. void splitname(int *done);
  44. void findvar(int *done);
  45. void duplockfromfh(int *done);
  46. void namefromlock(int *done);
  47. void namefromfh(int *done);
  48. void parentdir(int *done);
  49. void parentoffh(int *done);
  50. void examine(int *done);
  51. void exnext(int *done);
  52. void parsepattern(int *done);
  53. void matchpattern(int *done);
  54. void parsepatternnocase(int *done);
  55. void matchpatternnocase(int *done);
  56. void changemode(int *done);
  57. void openfromlock(int *done);
  58. void setfilesize(int *done);
  59. void isfilesystem(int *done);
  60. void getvar(int *done);
  61. void setvar(int *done);
  62. void setbuffer(int *done);
  63. void loadseg(int*done);
  64. void unloadseg(int*done);
  65. /* more commands can be _easily_ added */
  66.  
  67. struct commands {
  68.     char *name;
  69.     char *helpstr;
  70.     void (*rtn)(int *done);
  71. } comm[] = {
  72.     "?"," - list all commands",help,
  73.     "quit"," - exit this program",quit,
  74.     "Open","<filename> <mode> - open a file",open,
  75.     "Close","<file> - close a file",close,
  76.     "Read","<file> <offset> <length> - read bytes from file",myread,
  77.     "Write","<file> <offset> <length> - write bytes from file",mywrite,
  78.     "Seek","<file> <offset> <mode> - seek to position in file",myseek,
  79.     "SetFileSize","<file> <offset> <mode> - set the size of the file",
  80.         setfilesize,
  81.     "Lock","<name> <mode> - lock an object",lock,
  82.     "UnLock","<lock> - unlock a lock",unlock,
  83.     "DupLock","<lock> - duplicate a lock",duplock,
  84.     "SameLock","<lock> <lock> - determine if two locks are on same object",
  85.         samelock,
  86.     "ParentDir","<lock> - get a lock on the parent of a lock",parentdir,
  87.     "OpenFromLock","<lock> - open a filehandle on the locked item",
  88.         openfromlock,
  89.     "Examine","<lock> <offset> - examine a lock",examine,
  90.     "ExNext","<lock> <offset> - examine next file/dir",exnext,
  91.     "ParentOfFH","<file> - returns lock on parent dir of object",parentoffh,
  92.     "DupLockFromFH","<number> - get a lock on open file",duplockfromfh,
  93.     "NameFromLock","<lock> <offset> <length> - get name of object",
  94.         namefromlock,
  95.     "NameFromFH","<file> <offset> <length> - get name of object",
  96.         namefromfh,
  97.     "LockRecord","<file> <offset> <length> <mode> <timeout>- lock a part of a file",
  98.         lockrecord,
  99.     "UnlockRecord","<file> <offset> <length> - unlock a part of a file",
  100.         unlockrecord,
  101.     "ChangeMode","[FH|Lock] <#> <newmode> - change mode of item",
  102.         changemode,
  103.     "FGets","<file> <offset> <length> - buffered read to len or \\n",
  104.         myfgets,
  105.     "FPuts","<file> <offset>|<string> - buffered write of string",myfputs,
  106.     "FGetC","<file> <offset> - buffered character read",myfgetc,
  107.     "FPutC","<file> <offset>|<'char'> - buffered character write",myfputc,
  108.     "Flush","<file> - flush IO buffers for file",myflush,
  109.     "Execute","<command> <input file> <output file>",execute,
  110.     "DeleteFile","<filename> - delete a file or directory",deletefile,
  111.     "SplitName","<string> <'char'> <offset> <old pos> <size>",splitname,
  112.     "GetVar","<name> <offset> <size> <flags> - get a variable's value",
  113.         getvar,
  114.     "SetVar","<name> <offset> <size> <flags> - set a variable's value",
  115.         setvar,
  116.     "FindVar","<name> <type> - find a local variable",findvar,
  117.     "ParsePattern","<pattern> <offset> <len> - parse a pattern",
  118.         parsepattern,
  119.     "MatchPattern","<offset> <string> - match against a pattern",
  120.         matchpattern,
  121.     "ParsePatternNoCase","<pattern> <offset> <len> - parse a pattern",
  122.         parsepatternnocase,
  123.     "MatchPatternNoCase","<offset> <string> - match against a pattern",
  124.         matchpatternnocase,
  125.     "IsFileSystem","<string> - determine if a handler is a filesystem",
  126.         isfilesystem,
  127.     "LoadSeg","<file> - load an executable",loadseg,
  128.     "UnLoadSeg","0x<addr> - unload an executable",unloadseg,
  129.     "DumpFH","<file> - show filehandle",dumpfh,
  130.     "DumpLock","<lock> - show filelock",dumplock,
  131.     "ShowFiles"," - show all open files",showfiles,
  132.     "ShowLocks"," - show all locks",showlocks,
  133.     "Source","<file> - read commands from file",source,
  134.     "SetBuffer","[offset] [hex <hex digits>]|<string>",setbuffer,
  135.     "ShowBuffer","[hex] [offset [length]] - show part of buffer",
  136.         showbuffer,
  137. };
  138.  
  139. extern struct DosLibrary *DOSBase;
  140.  
  141. /* filehandle for input */
  142. FILE *fp = stdin;
  143.  
  144. int
  145. main (argc,argv)
  146.     int argc;
  147.     char **argv;
  148. {
  149.     char buff[256];
  150.     int done = FALSE,i,len;
  151.     char *command;
  152.  
  153.     if (argc != 1)
  154.     {
  155.         printf("usage: %s\n",argv[0]);
  156.         exit(10);
  157.     }
  158.  
  159.     if (DOSBase->dl_lib.lib_Version < 37)
  160.     {
  161.         printf("You need a dos.library V37 or better!\n");
  162.         exit(20);
  163.     }
  164.  
  165.     printf("%s 2.0 - use ? to get a list of commands\n\n",argv[0]);
  166.     printf("\tWarning: there is little error checking in this program.\n");
  167.     printf("You HAVE been warned!\n");
  168.  
  169.     while (!done)
  170.     {
  171.         if (fp == stdin)
  172.         {
  173.             printf("%s> ",argv[0]);
  174.             fflush(stdout);
  175.         }
  176.         if (fgets(buff,sizeof(buff),fp) == NULL)
  177.         {
  178.             if (fp == stdin)
  179.                 break;            /* eof */
  180.             else {
  181.                 fp = stdin;        /* end of 'source' */
  182.                 continue;
  183.             }
  184.         }
  185.  
  186.         len = strlen(buff);        /* kill \n */
  187.         if (len && buff[len-1] == '\n')
  188.             buff[len-1] = '\0';
  189.  
  190.         command = strtok(buff," ");
  191.         if (!command || !*command)        /* no command */
  192.             continue;
  193.  
  194.         for (i = 0; i < sizeof(comm)/sizeof(comm[0]); i++)
  195.         {
  196.             if (stricmp(command,comm[i].name) == SAME)
  197.             {
  198.                 /* found it - execute */
  199.  
  200.                 (*(comm[i].rtn))(&done);
  201.                 break;
  202.             }
  203.         }
  204.         if (i >= sizeof(comm)/sizeof(comm[0]))    /* failure */
  205.             printf("Error: command %s not known!\n",command);
  206.     }
  207.  
  208.     return 0;
  209. }
  210.  
  211. void
  212. help (int *done)
  213. {
  214.     int i;
  215.  
  216.     for (i = 0; i < sizeof(comm)/sizeof(comm[0]); i++)
  217.     {
  218.         printf(" %s %s\n",comm[i].name,comm[i].helpstr);
  219.     }
  220.     printf("\n <file> or <lock> refers to an index into the arrays\n");
  221.     printf(" <offset> refers to an offset into the buffer\n");
  222. }
  223.  
  224. void
  225. quit (int *done)
  226. {
  227.     *done = TRUE;
  228. }
  229.  
  230. void
  231. source (int *done)
  232. {
  233.     char *arg;
  234.     FILE *myfp;
  235.  
  236.     arg = strtok(NULL," ");
  237.     if (!arg)
  238.     {
  239.         printf("error: use source <filename>\n");
  240.         return;
  241.     }
  242.  
  243.     myfp = fopen(arg,"r");
  244.     if (!myfp)
  245.     {
  246.         printf("error: Can't open %s!\n",arg);
  247.         return;
  248.     }
  249.  
  250.     fp = myfp;
  251. }
  252.  
  253. unsigned char buffer[1024];
  254.  
  255. void
  256. showbuffer (int *done)
  257. {
  258.     char *arg;
  259.     int i;
  260.     int hex = FALSE;
  261.     long offset = 0;
  262.     long len    = 80;
  263.  
  264.     arg = strtok(NULL," ");
  265.     if (arg)
  266.     {
  267.         if (stricmp(arg,"hex") == SAME)
  268.         {
  269.             hex = TRUE;
  270.             arg = strtok(NULL," ");
  271.         }
  272.     }
  273.  
  274.     /* optional offset */
  275.     if (arg)
  276.     {
  277.         if (strlen(arg) != stcd_l(arg,&offset))
  278.         {
  279. err:            printf("error: bad number (%s)\n",arg);
  280.             printf("error: use showbuffer [hex] [offset [length]]\n");
  281.             return;
  282.         }
  283.  
  284.         arg = strtok(NULL," ");
  285.         if (arg)
  286.         {
  287.             if (strlen(arg) != stcd_l(arg,&len))
  288.             {
  289.                 goto err;
  290.             }
  291.         }
  292.     }
  293.  
  294.     for (i = 0; i < len; i++)
  295.     {
  296.         if (!hex)
  297.         {
  298.             if (!buffer[offset+i])
  299.                 break;
  300.             putchar(buffer[offset+i]);
  301.         } else {
  302.             if (i % 20 == 0)
  303.             {
  304.                 printf("\n%04ld: ",i+offset);
  305.             }
  306.             printf("%02x ",buffer[offset+i]);
  307.         }
  308.     }
  309.     putchar('\n');
  310. }
  311.  
  312. void
  313. setbuffer (int *done)
  314. {
  315.     char *str;
  316.     long offset;
  317.     char hexdigit[4];
  318.     long digit;
  319.  
  320.     str = strtok(NULL," ");
  321.     if (!str)
  322.     {
  323. err:        printf("Usage: SetBuffer [offset] [hex <hex digits>]|<string>\n");
  324.         return;
  325.     }
  326.     if (strlen(str) != stcd_l(str,&offset))
  327.     {
  328.         offset = 0;
  329.     } else {
  330.         /* got offset, get next string */
  331.         str = strtok(NULL," ");
  332.         if (!str)
  333.             goto err;
  334.     }
  335.  
  336.     if (stricmp(str,"hex") == SAME)
  337.     {
  338.         str = strtok(NULL," ");
  339.         if (!str)
  340.             goto err;
  341.  
  342.         while (*str)
  343.         {
  344.             hexdigit[0] = *str++;
  345.             if (!*str)
  346.                 goto err;
  347.  
  348.             hexdigit[1] = *str++;
  349.             hexdigit[2] = 0;
  350.  
  351.             if (stch_l(hexdigit,&digit) != 2)
  352.             {
  353.                 printf("Bad hex value '%s'!\n",hexdigit);
  354.                 return;
  355.             }
  356.  
  357.             buffer[offset++] = digit;
  358.         }
  359.     } else {
  360.         /* string */
  361.         strcpy(&(buffer[offset]),str);
  362.     }
  363. }
  364.  
  365. /* end standard code */
  366.  
  367. /* data */
  368.  
  369. BPTR files[10];
  370. BPTR locks[10];
  371. char *lastarg;        /* for getnum failures */
  372.  
  373. int
  374. getnum (long *num)
  375. {
  376.     lastarg = strtok(NULL," ");
  377.     if (!lastarg)
  378.         return FALSE;
  379.  
  380.     if (strlen(lastarg) != stcd_l(lastarg,num))
  381.     {
  382.         printf("error: bad number (%s)\n",lastarg);
  383.         return FALSE;
  384.     }
  385.  
  386.     return TRUE;
  387. }
  388.  
  389. int
  390. get2nums (long *num1, long *num2)
  391. {
  392.     if (!getnum(num1))
  393.         return FALSE;
  394.  
  395.     return getnum(num2);
  396. }
  397.  
  398. int
  399. get3nums (long *num1, long *num2, long *num3)
  400. {
  401.     if (!getnum(num1) || !getnum(num2))
  402.         return FALSE;
  403.  
  404.     return getnum(num3);
  405. }
  406.  
  407. /* open file */
  408. void
  409. open (int *done)
  410. {
  411.     char *name,*modestr;
  412.     LONG mode;
  413.     BPTR fh;
  414.     int i;
  415.  
  416.     name = strtok(NULL," ");
  417.     if (!name)
  418.     {
  419. err:        printf("error: use open <filename> <mode>\n");
  420.         return;
  421.     }
  422.  
  423.     modestr = strtok(NULL," ");
  424.     if (!modestr)
  425.         goto err;
  426.  
  427.     if (stricmp(modestr,"mode_oldfile") == SAME)
  428.         mode = MODE_OLDFILE;
  429.     else if (stricmp(modestr,"mode_newfile") == SAME)
  430.         mode = MODE_NEWFILE;
  431.     else
  432.         if (strlen(modestr) != stcd_l(modestr,&mode))
  433.         {
  434.             printf("error: bad number (%s)\n",modestr);
  435.             return;
  436.         }
  437.  
  438.     fh = Open(name,mode);
  439.     if (!fh)
  440.     {
  441.         printf("error: Can't open %s, ioerr = %ld\n",name,IoErr());
  442.         return;
  443.     }
  444.  
  445.     for (i = 0; i < sizeof(files)/sizeof(files[0]); i++)
  446.     {
  447.         if (files[i] == NULL)
  448.         {
  449.             files[i] = fh;
  450.             printf("Opened as file %d\n",i);
  451.             return;
  452.         }
  453.     }
  454.  
  455.     printf("no space for more files!\n");
  456.     Close(fh);
  457. }
  458.  
  459. void
  460. close (int *done)
  461. {
  462.     int num;
  463.  
  464.     if (!getnum(&num))
  465.     {
  466.         printf("error: use close <filenumber>\n");
  467.         return;
  468.     }
  469.  
  470.     Close(files[num]);
  471.     files[num] = NULL;
  472. }
  473.  
  474. void
  475. openfromlock (int *done)
  476. {
  477.     BPTR fh;
  478.     int i;
  479.  
  480.     if (!getnum(&i))
  481.     {
  482.         printf("error: use OpenFromLock <lock>\n");
  483.         return;
  484.     }
  485.  
  486.     fh = OpenFromLock(locks[i]);
  487.     if (!fh)
  488.     {
  489.         printf("error: Can't open from lock %d (0x%lx), IoErr = %ld\n",
  490.             i,locks[i],IoErr());
  491.         return;
  492.     }
  493.     locks[i] = NULL;
  494.  
  495.     for (i = 0; i < sizeof(files)/sizeof(files[0]); i++)
  496.     {
  497.         if (files[i] == NULL)
  498.         {
  499.             files[i] = fh;
  500.             printf("Opened as file %d\n",i);
  501.             return;
  502.         }
  503.     }
  504.  
  505.     printf("no space for more files!\n");
  506.     Close(fh);
  507. }
  508.  
  509. void
  510. showfiles (int *done)
  511. {
  512.     int i;
  513.  
  514.     printf("\tNumber\t   FH\n");
  515.     for (i = 0; i < sizeof(files)/sizeof(files[0]); i++)
  516.     {
  517.         if (files[i])
  518.             printf("\t%d\t- 0x%lx\n",i,files[i]);
  519.     }
  520. }
  521.  
  522. /* open file */
  523. void
  524. lock (int *done)
  525. {
  526.     char *name,*modestr;
  527.     LONG mode;
  528.     BPTR lock;
  529.     int i;
  530.  
  531.     name = strtok(NULL," ");
  532.     if (!name)
  533.     {
  534. err:        printf("error: use lock <name> <mode>\n");
  535.         return;
  536.     }
  537.  
  538.     modestr = strtok(NULL," ");
  539.     if (!modestr)
  540.         goto err;
  541.  
  542.     if (stricmp(modestr,"shared") == SAME)
  543.         mode = SHARED_LOCK;
  544.     else if (stricmp(modestr,"exclusive") == SAME)
  545.         mode = EXCLUSIVE_LOCK;
  546.     else
  547.         if (strlen(modestr) != stcd_l(modestr,&mode))
  548.         {
  549.             printf("error: bad number (%s)\n",modestr);
  550.             return;
  551.         }
  552.  
  553.     lock = Lock(name,mode);
  554.     if (!lock)
  555.     {
  556.         printf("error: Can't lock %s, ioerr = %ld\n",name,IoErr());
  557.         return;
  558.     }
  559.  
  560.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  561.     {
  562.         if (locks[i] == NULL)
  563.         {
  564.             locks[i] = lock;
  565.             printf("Locked as lock %d\n",i);
  566.             return;
  567.         }
  568.     }
  569.  
  570.     printf("no space for more locks!\n");
  571.     UnLock(lock);
  572. }
  573.  
  574. void
  575. unlock (int *done)
  576. {
  577.     int num;
  578.  
  579.     if (!getnum(&num))
  580.     {
  581.         printf("error: use unlock <locknumber>\n");
  582.         return;
  583.     }
  584.  
  585.     UnLock(locks[num]);
  586.     locks[num] = NULL;
  587. }
  588.  
  589. void
  590. duplock (int *done)
  591. {
  592.     int num,i;
  593.     BPTR lock;
  594.  
  595.     if (!getnum(&num))
  596.     {
  597.         printf("error: use duplock <locknumber>\n");
  598.         return;
  599.     }
  600.  
  601.     lock = DupLock(locks[num]);
  602.     if (!lock)
  603.     {
  604.         printf("error: Can't duplock 0x%lx, ioerr = %ld\n",
  605.             locks[num],IoErr());
  606.         printf(" (note: DupLock(0) == 0)\n");
  607.         return;
  608.     }
  609.  
  610.  
  611.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  612.     {
  613.         if (locks[i] == NULL)
  614.         {
  615.             locks[i] = lock;
  616.             printf("Locked as lock %d\n",i);
  617.             return;
  618.         }
  619.     }
  620.  
  621.     printf("no space for more locks!\n");
  622.     UnLock(lock);
  623. }
  624.  
  625. void
  626. parentdir (int *done)
  627. {
  628.     int num,i;
  629.     BPTR lock;
  630.  
  631.     if (!getnum(&num))
  632.     {
  633.         printf("error: use ParentDir <locknumber>\n");
  634.         return;
  635.     }
  636.  
  637.     lock = ParentDir(locks[num]);
  638.     if (!lock)
  639.     {
  640.         printf("error: Can't get parent of 0x%lx, ioerr = %ld\n",
  641.             locks[num],IoErr());
  642.         return;
  643.     }
  644.  
  645.  
  646.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  647.     {
  648.         if (locks[i] == NULL)
  649.         {
  650.             locks[i] = lock;
  651.             printf("Locked as lock %d\n",i);
  652.             return;
  653.         }
  654.     }
  655.  
  656.     printf("no space for more locks!\n");
  657.     UnLock(lock);
  658. }
  659.  
  660. void
  661. duplockfromfh (int *done)
  662. {
  663.     int num,i;
  664.     BPTR lock;
  665.  
  666.     if (!getnum(&num))
  667.     {
  668.         printf("error: use DupLockFromFH <filenumber>\n");
  669.         return;
  670.     }
  671.  
  672.     lock = DupLockFromFH(files[num]);
  673.     if (!lock)
  674.     {
  675.         printf("error: Can't duplockfromfh 0x%lx, ioerr = %ld\n",
  676.             files[num],IoErr());
  677.         return;
  678.     }
  679.  
  680.  
  681.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  682.     {
  683.         if (locks[i] == NULL)
  684.         {
  685.             locks[i] = lock;
  686.             printf("Locked as lock %d\n",i);
  687.             return;
  688.         }
  689.     }
  690.  
  691.     printf("no space for more locks!\n");
  692.     UnLock(lock);
  693. }
  694.  
  695. void
  696. parentoffh (int *done)
  697. {
  698.     int num,i;
  699.     BPTR lock;
  700.  
  701.     if (!getnum(&num))
  702.     {
  703.         printf("error: use ParentOfFH <filenumber>\n");
  704.         return;
  705.     }
  706.  
  707.     lock = ParentOfFH(files[num]);
  708.     if (!lock)
  709.     {
  710.         printf("error: Can't get parent of 0x%lx, ioerr = %ld\n",
  711.             files[num],IoErr());
  712.         return;
  713.     }
  714.  
  715.  
  716.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  717.     {
  718.         if (locks[i] == NULL)
  719.         {
  720.             locks[i] = lock;
  721.             printf("Locked as lock %d\n",i);
  722.             return;
  723.         }
  724.     }
  725.  
  726.     printf("no space for more locks!\n");
  727.     UnLock(lock);
  728. }
  729.  
  730. void
  731. examine (int *done)
  732. {
  733.     struct FileInfoBlock *fib;
  734.     LONG num,offset,rc;
  735.  
  736.     if (!getnum(&num))
  737.     {
  738. err:        printf("error: use Examine <lock> <offset>\n");
  739.         return;
  740.     }
  741.  
  742.     if (!getnum(&offset))
  743.         goto err;
  744.  
  745.     fib = (void *) &buffer[offset];
  746.  
  747.     rc = Examine(locks[num],fib);
  748.     printf("Examine returned %ld, ioerr = %ld\n",rc,IoErr());
  749. }
  750.  
  751. void
  752. exnext (int *done)
  753. {
  754.     struct FileInfoBlock *fib;
  755.     LONG num,offset,rc;
  756.  
  757.     if (!getnum(&num))
  758.     {
  759. err:        printf("error: use ExNext <lock> <offset>\n");
  760.         return;
  761.     }
  762.  
  763.     if (!getnum(&offset))
  764.         goto err;
  765.  
  766.     fib = (void *) &buffer[offset];
  767.  
  768.     rc = ExNext(locks[num],fib);
  769.     printf("ExNext returned %ld, ioerr = %ld\n",rc,IoErr());
  770. }
  771.  
  772. void
  773. showlocks (int *done)
  774. {
  775.     int i;
  776.  
  777.     printf("\tNumber\t   Lock\n");
  778.     for (i = 0; i < sizeof(locks)/sizeof(locks[0]); i++)
  779.     {
  780.         if (locks[i])
  781.             printf("\t%d\t- 0x%lx\n",i,locks[i]);
  782.     }
  783. }
  784.  
  785. void
  786. myread (int *done)
  787. {
  788.     long offset,length,file,len;
  789.  
  790.     if (!get3nums(&file,&offset,&length))
  791.     {
  792.         printf("error: use read <filenumber> <offset> <length>\n");
  793.         return;
  794.     }
  795.  
  796.     len = Read(files[file],&(buffer[offset]),length);
  797.     if (len < 0)
  798.     {
  799.         printf("error on read - %ld\n",IoErr());
  800.         return;
  801.     }
  802.  
  803.     printf("Read %ld characters\n",len);
  804. }
  805.  
  806. void
  807. mywrite (int *done)
  808. {
  809.     long offset,length,file,len;
  810.  
  811.     if (!get3nums(&file,&offset,&length))
  812.     {
  813.         printf("error: use write <filenumber> <offset> <length>\n");
  814.         return;
  815.     }
  816.  
  817.     len = Write(files[file],&(buffer[offset]),length);
  818.     if (len < 0)
  819.     {
  820.         printf("error on write - %ld\n",IoErr());
  821.         return;
  822.     }
  823.  
  824.     printf("Wrote %ld characters\n",len);
  825. }
  826.  
  827. void
  828. myseek (int *done)
  829. {
  830.     long offset,mode,file,oldpos;
  831.  
  832.     if (!get3nums(&file,&offset,&mode))
  833.     {
  834.         printf("error: use seek <filenumber> <offset> <mode>\n");
  835.         return;
  836.     }
  837.  
  838.     oldpos = Seek(files[file],offset,mode);
  839.     if (oldpos < 0)
  840.     {
  841.         printf("error on seek - %ld\n",IoErr());
  842.         return;
  843.     }
  844.  
  845.     printf("old position was %ld\n",oldpos);
  846. }
  847.  
  848. void
  849. setfilesize (int *done)
  850. {
  851.     long offset,mode,file,oldpos;
  852.  
  853.     if (!get3nums(&file,&offset,&mode))
  854.     {
  855.         printf("error: use SetFileSize <filenumber> <offset> <mode>\n");
  856.         return;
  857.     }
  858.  
  859.     oldpos = SetFileSize(files[file],offset,mode);
  860.     if (oldpos < 0)
  861.     {
  862.         printf("error on setfilesize - %ld\n",IoErr());
  863.         return;
  864.     }
  865.  
  866.     printf("old position was %ld, current position is %ld\n",oldpos,
  867.         Seek(files[file],0,OFFSET_CURRENT));
  868. }
  869.  
  870. void
  871. lockrecord (int *done)
  872. {
  873.     long offset,length,file,res,mode,timeout = 50;
  874.  
  875.     if (!get3nums(&file,&offset,&length) ||
  876.         !get2nums(&mode,&timeout))
  877.     {
  878.         printf(
  879.  "error: use lockrecord <filenumber> <offset> <length> <mode> <timeout>\n");
  880.         return;
  881.     }
  882.  
  883.     res = LockRecord(files[file],offset,length,mode,timeout);
  884.     if (res == 0)
  885.     {
  886.         printf("error on lock - %ld\n",IoErr());
  887.         return;
  888.     }
  889.  
  890.     printf("Locked (%ld,%ld)\n",res,IoErr());
  891. }
  892.  
  893. void
  894. unlockrecord (int *done)
  895. {
  896.     long offset,length,file,res;
  897.  
  898.     if (!get3nums(&file,&offset,&length))
  899.     {
  900.      printf("error: use unlockrecord <filenumber> <offset> <length>\n");
  901.      return;
  902.     }
  903.  
  904.     res = UnLockRecord(files[file],offset,length);
  905.     if (res == 0)
  906.     {
  907.         printf("error on unlock - %ld\n",IoErr());
  908.         return;
  909.     }
  910.  
  911.     printf("Unlocked\n");
  912. }
  913.  
  914. void
  915. myfgets (int *done)
  916. {
  917.     long offset,length,file;
  918.     char *p;
  919.  
  920.     if (!get3nums(&file,&offset,&length))
  921.     {
  922.         printf("error: use fgets <filenumber> <offset> <length>\n");
  923.         return;
  924.     }
  925.  
  926.     p = (char *) FGets(files[file],&(buffer[offset]),length);
  927.     if (p == 0)
  928.     {
  929.         printf("eof or error on FGets - %ld\n",IoErr());
  930.         return;
  931.     }
  932.  
  933.     printf("Read %ld characters\n",strlen(p));
  934. }
  935.  
  936. void
  937. myfputs (int *done)
  938. {
  939.     long offset,file;
  940.     unsigned char *p;
  941.  
  942.     if (!getnum(&file))
  943.     {
  944. err:        printf("error: use fputs <filenumber> <offset>|<string>\n");
  945.         return;
  946.     }
  947.     if (!(p = strtok(NULL," ")))
  948.         goto err;
  949.  
  950.     if (strlen(p) == stcd_l(p,&offset))
  951.     {
  952.         p = &(buffer[offset]);
  953.     }
  954.  
  955.     if (FPuts(files[file],p))
  956.     {
  957.         printf("eof or error on FPuts - %ld\n",IoErr());
  958.         return;
  959.     }
  960.  
  961.     printf("Wrote %ld characters\n",strlen(p));
  962. }
  963.  
  964. void
  965. myfgetc (int *done)
  966. {
  967.     long offset,file;
  968.     long p;
  969.  
  970.     if (!get2nums(&file,&offset))
  971.     {
  972.         printf("error: use fgetc <filenumber> <offset>\n");
  973.         return;
  974.     }
  975.  
  976.     p = FGetC(files[file]);
  977.     if (p == -1)
  978.     {
  979.         printf("eof on FGetC - %ld\n",IoErr());
  980.         return;
  981.     }
  982.  
  983.     buffer[offset] = p;
  984.  
  985.     printf("Read 0x%lx\n",p);
  986. }
  987.  
  988. void
  989. myfputc (int *done)
  990. {
  991.     long offset,file;
  992.     unsigned char *p;
  993.  
  994.     if (!getnum(&file))
  995.     {
  996. err:        printf("error: use fputc <filenumber> <offset>|<'char'>\n");
  997.         return;
  998.     }
  999.     if (!(p = strtok(NULL," ")))
  1000.         goto err;
  1001.  
  1002.     if (strlen(p) == stcd_l(p,&offset))
  1003.     {
  1004.         p = &(buffer[offset]);
  1005.     }
  1006.  
  1007.     FPutC(files[file],*p);
  1008.  
  1009.     printf("Wrote 0x%lx\n",*p);
  1010. }
  1011.  
  1012. void
  1013. myflush (int *done)
  1014. {
  1015.     long file;
  1016.  
  1017.     if (!getnum(&file))
  1018.     {
  1019.         printf("error: use flush <filenumber>\n");
  1020.         return;
  1021.     }
  1022.  
  1023.     Flush(files[file]);
  1024.  
  1025.     printf("flushed\n");
  1026. }
  1027.  
  1028. void
  1029. dumpfh (int *done)
  1030. {
  1031.     long file;
  1032.     struct FileHandle *fh;
  1033.  
  1034.     if (!getnum(&file))
  1035.     {
  1036.         printf("error: use dumpfh <filenumber>\n");
  1037.         return;
  1038.     }
  1039.  
  1040.     fh = (void *) BADDR(files[file]);
  1041.     printf("link = 0x%lx, port = 0x%lx, type = 0x%lx\n",
  1042.         fh->fh_Link,fh->fh_Port,fh->fh_Type);
  1043.     printf("buf = 0x%lx, pos = %ld, end = %ld\n",
  1044.         fh->fh_Buf,fh->fh_Pos,fh->fh_End);
  1045.     printf("func1 = 0x%lx, func2 = 0x%lx, func3 = 0x%lx\n",
  1046.         fh->fh_Func1,fh->fh_Func2,fh->fh_Func3);
  1047.     printf("fh_Arg1 = 0x%lx, fh-Arg2 = 0x%lx\n",
  1048.         fh->fh_Arg1,fh->fh_Arg2);
  1049. }
  1050.  
  1051. void
  1052. dumplock (int *done)
  1053. {
  1054.     long num;
  1055.     struct FileLock *lock;
  1056.  
  1057.     if (!getnum(&num))
  1058.     {
  1059.         printf("error: use dumplock <locknumber>\n");
  1060.         return;
  1061.     }
  1062.  
  1063.     lock = (void *) BADDR(locks[num]);
  1064.     if (!lock)
  1065.     {
  1066.         printf("lock on boot fs root (NULL)\n");
  1067.         return;
  1068.     }
  1069.     printf("link = 0x%lx, key = 0x%lx, access = 0x%lx\n",
  1070.         lock->fl_Link,lock->fl_Key,lock->fl_Access);
  1071.     printf("port = 0x%lx, volume = %ld (%s)\n",
  1072.         lock->fl_Task,lock->fl_Volume, (lock->fl_Volume ?
  1073.             ((char *) BADDR(((struct DosList *)
  1074.                     BADDR(lock->fl_Volume))->dol_Name))+1 :
  1075.             ""));
  1076. }
  1077.  
  1078. void
  1079. samelock (int *done)
  1080. {
  1081.     LONG rc;
  1082.     BPTR lock1,lock2;
  1083.  
  1084.     if (!get2nums(&lock1,&lock2))
  1085.     {
  1086.         printf("error: use samelock <lock1> <lock2>\n");
  1087.         return;
  1088.     }
  1089.  
  1090.     rc = SameLock(locks[lock1],locks[lock2]);
  1091.  
  1092.     printf("SameLock returned %ld\n",rc);
  1093. }
  1094.  
  1095. void
  1096. changemode (int *done)
  1097. {
  1098.     LONG rc,type,num,mode;
  1099.     char *str;
  1100.  
  1101.     str = strtok(NULL," ");
  1102.     if (!str)
  1103.     {
  1104. err:        printf("error: use ChangeMode FH|Lock <number> <newmode>\n");
  1105.         return;
  1106.     }
  1107.  
  1108.     if (stricmp(str,"fh") == SAME)
  1109.         type = CHANGE_FH;
  1110.     else if (stricmp(str,"lock") == SAME)
  1111.         type = CHANGE_LOCK;
  1112.     else {
  1113.         printf("'%s' is not a valid type\n",str);
  1114.         goto err;
  1115.     }
  1116.  
  1117.     if (!get2nums(&num,&mode))
  1118.         goto err;
  1119.  
  1120.     rc = ChangeMode(type,type == CHANGE_FH ? files[num] : locks[num],
  1121.             mode);
  1122.  
  1123.     printf("ChangeMode returned %ld, ioerr = %ld\n",rc,IoErr());
  1124. }
  1125.  
  1126. void
  1127. execute (int *done)
  1128. {
  1129.     char *command;
  1130.     long input,output;
  1131.     long rc;
  1132.     char buf[256];
  1133.  
  1134.     command = strtok(NULL," ");
  1135.     if (!command)
  1136.     {
  1137. err:        printf("error: use execute <command> <input fh> <output fh>\n");
  1138.         return;
  1139.     }
  1140.     if (*command == '"')
  1141.     {
  1142.         /* strip quotes */
  1143.         command++;
  1144.         if (command[strlen(command)-1] == '"')
  1145.         {
  1146.             command[strlen(command)-1] = 0;
  1147.         } else {
  1148.             strcpy(buf,command);
  1149.             do {
  1150.                 command = strtok(NULL," ");
  1151.                 if (!command)
  1152.                     goto err;
  1153.                 strcat(buf,command);
  1154.             } while (command[strlen(command)-1] != '"');
  1155.             buf[strlen(buf)-1] = 0;
  1156.             command = buf;
  1157.         }
  1158.     }
  1159.  
  1160.     if (!get2nums(&input,&output))
  1161.         goto err;
  1162.  
  1163.     if (input == -1)
  1164.         input = NULL;
  1165.     else
  1166.         input = files[input];
  1167.  
  1168.     if (output == -1)
  1169.         output = NULL;
  1170.     else
  1171.         output = files[output];
  1172.  
  1173.     printf("Execute(\"%s\",0x%lx,0x%lx)\n",command,input,output);
  1174.  
  1175.     rc = Execute(command,input,output);
  1176.  
  1177.     printf("Execute returned %ld (ioerr = %ld)\n",rc,IoErr());
  1178. }
  1179.  
  1180. void
  1181. deletefile (int *done)
  1182. {
  1183.     LONG rc;
  1184.  
  1185.     rc = DeleteFile(strtok(NULL," "));
  1186.  
  1187.     printf("DeleteFile returned %ld, ioerr = %ld\n",rc,IoErr());
  1188. }
  1189.  
  1190. void
  1191. splitname (int *done)
  1192. {
  1193.     char *string,*separator;
  1194.     unsigned char *buf;
  1195.     LONG size,pos,offset;
  1196.  
  1197.     string = strtok(NULL," ");
  1198.     if (!string)
  1199.     {
  1200. err:    printf("usage: SplitName <string> <'char'> <offset> <old pos> <size>\n");
  1201.     return;
  1202.     }
  1203.  
  1204.     separator = strtok(NULL," ");
  1205.     if (!separator)
  1206.         goto err;
  1207.  
  1208.     if (!getnum(&offset))
  1209.         goto err;
  1210.  
  1211.     buf = &(buffer[offset]);
  1212.  
  1213.     if (!getnum(&pos))
  1214.         goto err;
  1215.  
  1216.     if (!getnum(&size))
  1217.         goto err;
  1218.  
  1219.     printf("SplitName(%s,%lc,0x%lx,%ld,%ld)\n",
  1220.         string,*separator,buf,pos,size);
  1221.     pos = SplitName(string,*separator,buf,(UWORD) pos,size);
  1222.     printf("  returned %ld (ioerr = %ld)\n",pos,IoErr());
  1223.  
  1224. }
  1225.  
  1226. void
  1227. getvar (int *done)
  1228. {
  1229.     char *name;
  1230.     LONG offset,size,flags,len;
  1231.  
  1232.     name = strtok(NULL," ");
  1233.     if (!name)
  1234.     {
  1235. err:        printf("usage: GetVar <name> <offset> <size> <flags>\n");
  1236.         return;
  1237.     }
  1238.  
  1239.     if (!get3nums(&offset,&size,&flags))
  1240.         goto err;
  1241.  
  1242.     len = GetVar(name,&(buffer[offset]),size,flags);
  1243.     printf("GetVar returned %ld (string = %s)\n",len,&(buffer[offset]));
  1244. }
  1245.  
  1246. void
  1247. setvar (int *done)
  1248. {
  1249.     char *name;
  1250.     LONG offset,size,flags,rc;
  1251.  
  1252.     name = strtok(NULL," ");
  1253.     if (!name)
  1254.     {
  1255. err:        printf("usage: SetVar <name> <offset> <size> <flags>\n");
  1256.         return;
  1257.     }
  1258.  
  1259.     if (!get3nums(&offset,&size,&flags))
  1260.         goto err;
  1261.  
  1262.     rc = SetVar(name,&(buffer[offset]),size,flags);
  1263.     if (!rc)
  1264.         printf("SetVar failed!  (IoErr = %ld)\n",IoErr());
  1265. }
  1266.  
  1267. void
  1268. findvar (int *done)
  1269. {
  1270.     char *name;
  1271.     LONG type;
  1272.     struct LocalVar *lv;
  1273.  
  1274.     name = strtok(NULL," ");
  1275.     if (!name)
  1276.     {
  1277. err:        printf("usage: FindVar <name> <type>\n");
  1278.         return;
  1279.     }
  1280.  
  1281.     if (!getnum(&type))
  1282.         goto err;
  1283.  
  1284.     lv = FindVar(name,type);
  1285.     printf("FindVar(%s,%ld) returned 0x%lx\n",name,type,lv);
  1286. }
  1287.  
  1288. void
  1289. namefromlock (int *done)
  1290. {
  1291.     unsigned char *buf;
  1292.     LONG size,num,offset,rc;
  1293.  
  1294.     if (!getnum(&num))
  1295.     {
  1296. err:    printf("usage: NameFromLock <lock> <offset> <length>\n");
  1297.     return;
  1298.     }
  1299.  
  1300.     if (!getnum(&offset))
  1301.         goto err;
  1302.  
  1303.     buf = &(buffer[offset]);
  1304.  
  1305.     if (!getnum(&size))
  1306.         goto err;
  1307.  
  1308.     rc = NameFromLock(locks[num],buf,size);
  1309.     printf("NameFromLock returned %ld (ioerr = %ld)\n",rc,IoErr());
  1310.  
  1311.     if (rc)
  1312.         printf("\t\"%s\"\n",buf);
  1313. }
  1314.  
  1315. void
  1316. namefromfh (int *done)
  1317. {
  1318.     unsigned char *buf;
  1319.     LONG size,num,offset,rc;
  1320.  
  1321.     if (!getnum(&num))
  1322.     {
  1323. err:    printf("usage: NameFromFH <file> <offset> <length>\n");
  1324.     return;
  1325.     }
  1326.  
  1327.     if (!getnum(&offset))
  1328.         goto err;
  1329.  
  1330.     buf = &(buffer[offset]);
  1331.  
  1332.     if (!getnum(&size))
  1333.         goto err;
  1334.  
  1335.     rc = NameFromFH(files[num],buf,size);
  1336.     printf("NameFromFH returned %ld (ioerr = %ld)\n",rc,IoErr());
  1337.  
  1338.     if (rc)
  1339.         printf("\t\"%s\"\n",buf);
  1340. }
  1341.  
  1342. void
  1343. parsepattern (int *done)
  1344. {
  1345.     char *pattern;
  1346.     char *dest;
  1347.     LONG offset,len,rc;
  1348.  
  1349.     pattern = strtok(NULL," ");
  1350.     if (!pattern)
  1351.     {
  1352. err:        printf("usage: ParsePattern <pattern> <offset> <len>\n");
  1353.         return;
  1354.     }
  1355.  
  1356.     if (!get2nums(&offset,&len))
  1357.         goto err;
  1358.  
  1359.     dest = &(buffer[offset]);
  1360.  
  1361.     rc = ParsePattern(pattern,dest,len);
  1362.  
  1363.     printf("ParsePattern returned %ld\n",rc);
  1364. }
  1365.  
  1366. void
  1367. matchpattern (int *done)
  1368. {
  1369.     char *pattern;
  1370.     char *string;
  1371.     LONG offset,rc;
  1372.  
  1373.     if (!getnum(&offset))
  1374.         goto err;
  1375.  
  1376.     string = strtok(NULL," ");
  1377.     if (!string)
  1378.     {
  1379. err:        printf("usage: MatchPattern <offset> <string>\n");
  1380.         return;
  1381.     }
  1382.  
  1383.     pattern = &(buffer[offset]);
  1384.  
  1385.     rc = MatchPattern(pattern,string);
  1386.  
  1387.     printf("MatchPattern returned %ld\n",rc);
  1388. }
  1389.  
  1390. void
  1391. parsepatternnocase (int *done)
  1392. {
  1393.     char *pattern;
  1394.     char *dest;
  1395.     LONG offset,len,rc;
  1396.  
  1397.     pattern = strtok(NULL," ");
  1398.     if (!pattern)
  1399.     {
  1400. err:        printf("usage: ParsePatternNoCase <pattern> <offset> <len>\n");
  1401.         return;
  1402.     }
  1403.  
  1404.     if (!get2nums(&offset,&len))
  1405.         goto err;
  1406.  
  1407.     dest = &(buffer[offset]);
  1408.  
  1409.     rc = ParsePatternNoCase(pattern,dest,len);
  1410.  
  1411.     printf("ParsePatternNoCase returned %ld\n",rc);
  1412. }
  1413.  
  1414. void
  1415. matchpatternnocase (int *done)
  1416. {
  1417.     char *pattern;
  1418.     char *string;
  1419.     LONG offset,rc;
  1420.  
  1421.     if (!getnum(&offset))
  1422.         goto err;
  1423.  
  1424.     string = strtok(NULL," ");
  1425.     if (!string)
  1426.     {
  1427. err:        printf("usage: MatchPatternNoCase <offset> <string>\n");
  1428.         return;
  1429.     }
  1430.  
  1431.     pattern = &(buffer[offset]);
  1432.  
  1433.     rc = MatchPatternNoCase(pattern,string);
  1434.  
  1435.     printf("MatchPatternNoCase returned %ld\n",rc);
  1436. }
  1437.  
  1438. void
  1439. isfilesystem (int *done)
  1440. {
  1441.     char *string;
  1442.     LONG rc;
  1443.  
  1444.     string = strtok(NULL," ");
  1445.     if (!string)
  1446.     {
  1447.         printf("usage: IsFileSystem <device name>:\n");
  1448.         return;
  1449.     }
  1450.  
  1451.     rc = IsFileSystem(string);
  1452.  
  1453.     printf("IsFileSystem returned %ld\n",rc);
  1454. }
  1455.  
  1456. void
  1457. loadseg (int *done)
  1458. {
  1459.     BPTR seg;
  1460.     char *string;
  1461.  
  1462.     string = strtok(NULL," ");
  1463.     if (!string)
  1464.     {
  1465.         printf("usage: LoadSeg <file>\n");
  1466.         return;
  1467.     }
  1468.  
  1469.     seg = LoadSeg(string);
  1470.     printf("LoadSeg returned 0x%lx, ioerr = %ld\n",seg,IoErr());
  1471. }
  1472.  
  1473. void
  1474. unloadseg (int *done)
  1475. {
  1476.     BPTR seg;
  1477.     char *string;
  1478.  
  1479.     string = strtok(NULL," ");
  1480.     if (!string)
  1481.     {
  1482. err:        printf("usage: UnLoadSeg 0x<addr>\n");
  1483.         return;
  1484.     }
  1485.  
  1486.     if (*string++ != '0' || *string++ != 'x')
  1487.         goto err;
  1488.  
  1489.     if (stch_l(string,&seg) != strlen(string))
  1490.         goto err;
  1491.  
  1492.     UnLoadSeg(seg);
  1493.  
  1494.     printf("unloaded 0x%lx\n",seg);
  1495. }
  1496.